//
//  landingViewController.m
//  WHB_driver
//
//  Created by 徐国庆 on 15/12/28.
//  Copyright (c) 2015年 turing. All rights reserved.
//

#import "landingViewController.h"
#import "ForgetPassWordViewController.h"
#import "User.h"

@interface landingViewController ()<UITextFieldDelegate>

@property(nonatomic, strong)UITextField *phoneNumberTextField;
@property(nonatomic, strong)UITextField *passWordTextField;

@end

@implementation landingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initWithView];
}

#pragma mark 登陆点击事件
- (void)zoomInAction:(UIButton *)button{
    
    [self userLogin];
}

#pragma mark 忘记密码点击事件
- (void)gorgetPassWordButtonAction:(UIButton *)button{
    NSLog(@"忘记密码");
    ForgetPassWordViewController *forgetPassWordVC = [[ForgetPassWordViewController alloc] init];
//    [self.navigationController pushViewController:forgetPassWordVC animated:YES];
    [self presentViewController:forgetPassWordVC animated:YES completion:^{
        
    }];
}

/**
 *  初始化视图
 */
- (void)initWithView{
    
    // 底图
    UIImageView *bottomImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    bottomImageView.image = [UIImage imageNamed:@"BG_Home-1"];
    [self.view addSubview:bottomImageView];
    
    // logo 130 * 61
    UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kUIScreenWidth - 130) / 2, 238 / 2, 130, 61)];
    logoImageView.image = [UIImage imageNamed:@"Logo-1"];
    [self.view addSubview:logoImageView];
    
    // 电话号图标  34 * 34
    UIImageView *photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(45, CGRectGetMaxY(logoImageView.frame) + 20, 34, 34)];
    photoImageView.image = [UIImage imageNamed:@"icon_Box_user"];
    [self.view addSubview:photoImageView];
    
    // 电话号码
    self.phoneNumberTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(photoImageView.frame), photoImageView.frame.origin.y, 200, photoImageView.frame.size.height)];
    [self.phoneNumberTextField setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
    UIColor *color = [UIColor HexStrWithColor:@"999999"];
    self.phoneNumberTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName: color}];
    self.phoneNumberTextField.secureTextEntry = NO; //密码
    self.phoneNumberTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    self.phoneNumberTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.phoneNumberTextField.returnKeyType = UIReturnKeyDone;
    self.phoneNumberTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    self.phoneNumberTextField.delegate = self;
    self.phoneNumberTextField.backgroundColor = [UIColor clearColor];
    self.phoneNumberTextField.textColor = [UIColor HexStrWithColor:@"999999"];
    [self.view addSubview:self.phoneNumberTextField];
    
    // 电话号下线
    UILabel *line1Label = [[UILabel alloc] initWithFrame:CGRectMake(35, CGRectGetMaxY(photoImageView.frame), (kUIScreenWidth - 70), 1)];
    line1Label.backgroundColor = [UIColor HexStrWithColor:@"999999"];
    [self.view addSubview:line1Label];
    
    // 密码图标 34 * 34
    UIImageView *passwordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(45, CGRectGetMaxY(photoImageView.frame) + 20, 34, 34)];
    passwordImageView.image = [UIImage imageNamed:@"icon_Box_password"];
    [self.view addSubview:passwordImageView];

    // 密码
    self.passWordTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(photoImageView.frame), passwordImageView.frame.origin.y, 200, photoImageView.frame.size.height)];
    [self.passWordTextField setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型
    UIColor *color2 = [UIColor HexStrWithColor:@"999999"];
    self.passWordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"密码" attributes:@{NSForegroundColorAttributeName: color2}];
    self.passWordTextField.secureTextEntry = YES; //密码
    self.passWordTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    self.passWordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.passWordTextField.returnKeyType = UIReturnKeyDone;
//    self.passWordTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    self.passWordTextField.delegate = self;
    self.passWordTextField.backgroundColor = [UIColor clearColor];
    self.passWordTextField.textColor = [UIColor HexStrWithColor:@"999999"];
    [self.view addSubview:self.passWordTextField];

    // 密码下线
    UILabel *line2Label = [[UILabel alloc] initWithFrame:CGRectMake(35, CGRectGetMaxY(passwordImageView.frame), (kUIScreenWidth - 70), 1)];
    line2Label.backgroundColor = [UIColor HexStrWithColor:@"999999"];
    [self.view addSubview:line2Label];

    // 锁图标 34 * 34
    UIImageView *keyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(line2Label.frame) - 40, passwordImageView.frame.origin.y, 34, 34)];
    keyImageView.image = [UIImage imageNamed:@"button_Password_eye_grey_closed"];
    [self.view addSubview:keyImageView];
    
    // 登陆按钮
    UIButton *loadingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loadingButton.frame = CGRectMake(104 / 2, CGRectGetMaxY(line2Label.frame) + 35, (kUIScreenWidth - 104), 38);
    loadingButton.layer.masksToBounds = YES;
    loadingButton.layer.cornerRadius = 19;
    loadingButton.layer.borderWidth = 1.5;
    loadingButton.layer.borderColor = [UIColor HexStrWithColor:@"f8cc00"].CGColor;
    [loadingButton setTitle:@"登陆" forState:UIControlStateNormal];
    [loadingButton setTitleColor:[UIColor HexStrWithColor:@"f8cc00"] forState:UIControlStateNormal];
    [loadingButton addTarget:self action:@selector(zoomInAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loadingButton];
    
    //设置button下划线
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"忘记密码"];
    NSRange strRange = {0,[str length]};
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor HexStrWithColor:@"999999"] range:strRange];  //设置颜色
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    
    // 忘记密码
    UIButton *forgetPasswordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    forgetPasswordButton.frame = CGRectMake(104 / 2, CGRectGetMaxY(loadingButton.frame) + 15, (kUIScreenWidth - 104), 38);
    [forgetPasswordButton setAttributedTitle:str forState:UIControlStateNormal];
//    [forgetPasswordButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetPasswordButton setTitleColor:[UIColor HexStrWithColor:@"999999"] forState:UIControlStateNormal];
    [forgetPasswordButton addTarget:self action:@selector(gorgetPassWordButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetPasswordButton];
    
    // “司机版”
    UILabel *driverLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kUIScreenHeight - 40, kUIScreenWidth, 20)];
    driverLabel.textColor = [UIColor HexStrWithColor:@"f8cc00"];
    driverLabel.text = @"司机版";
    driverLabel.textAlignment = 1;
    driverLabel.font = [UIFont systemFontOfSize:15 weight:2];
    [self.view addSubview:driverLabel];
}

- (void)userLogin{
    NSString *tempStr = [UtilsHelper MD5:@"123456"];
    [DataController userLogin:@"13704800000"
                     password:tempStr
                    loginType:@"3"
                         imei:kIdentifierString
                    withBlock:^(NSDictionary *result) {
                        NSLog(@"result == %@ == %@", result, [result objectForKey:@"resMsg"]);
                        if ([[result objectForKey:@"resCode"] isEqualToString:@"0"]) {
                            NSDictionary *data = [result objectForKey:@"data"];
                            User *user = [MTLJSONAdapter modelOfClass:[User class] fromJSONDictionary:data error:nil];
                            //持久化用户登录信息
                            [NSKeyedArchiver archiveRootObject:user toFile:kUserPath];
                            [self dismissViewControllerAnimated:YES completion:^{
                                
                            }];
                        } else if ([[result objectForKey:@"resCode"] isEqualToString:@"3"]){
                            NSLog(@"密码输入错误三次!");
                        }
                    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
